package com.example.imgup;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.clearText;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.replaceText;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.intent.Intents.intended;
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;

import androidx.test.espresso.intent.Intents;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class RegisterActivityTest {
int t=0;
    @Rule
    public ActivityScenarioRule<RegisterActivity> activityRule = new ActivityScenarioRule<>(RegisterActivity.class);

    @Before
    public void setUp() {
        Intents.init();
    }

    @After
    public void tearDown() {
        Intents.release();
    }

    @Test
    public void testRegisterActivityLaunch() {
        // 确保注册页面正确启动
        onView(withId(R.id.mregister)).check(matches(isDisplayed()));
    }

    @Test
    public void testEmptyFieldValidation() {
        // 清空输入框
        onView(withId(R.id.rusername)).perform(clearText());
        onView(withId(R.id.rpassword)).perform(clearText());
        onView(withId(R.id.rphone)).perform(clearText());
        onView(withId(R.id.remil)).perform(clearText());

        // 尝试点击注册按钮
        onView(withId(R.id.mregister)).perform(click());

        // 验证是否有相应的提示信息
        onView(withText("账号不能为空！")).check(matches(isDisplayed()));
        onView(withText("密码不能为空！")).check(matches(isDisplayed()));
        onView(withText("电话不能为空!")).check(matches(isDisplayed()));
        onView(withText("邮箱不能为空！")).check(matches(isDisplayed()));
    }

    @Test
    public void testRegistrationSuccess() {
        // 输入有效的注册信息
        onView(withId(R.id.rusername)).perform(typeText("111"));
        onView(withId(R.id.rpassword)).perform(typeText("123"));
        onView(withId(R.id.rphone)).perform(typeText("13198124199"));
        onView(withId(R.id.remil)).perform(typeText("3244023708@qq.com"));

        // 点击注册按钮
        onView(withId(R.id.mregister)).perform(click());

        // 验证是否成功提示“注册成功”
        onView(withText("注册成功")).check(matches(isDisplayed()));

        t=1;
    }

    @Test
    public void testBackToLogin() {
        // 模拟注册成功后返回登录页面
        // 注意：这个测试假设注册成功后会自动返回登录页面，实际情况可能需要根据实际逻辑调整
        onView(withId(R.id.fh)).perform(click());
        intended(hasComponent(LoginActivity.class.getName()));

        if(t==1) {
            // 输入有效的用户名和密码，并关闭软键盘
            onView(withId(R.id.lusername)).perform(replaceText("111"), closeSoftKeyboard());
            onView(withId(R.id.lpassword)).perform(replaceText("123"), closeSoftKeyboard());

            // 点击登录按钮
            onView(withId(R.id.login)).perform(click());

            // 等待一段时间确保Intent有足够时间处理（根据实际情况调整等待时间）
            try {
                Thread.sleep(3000); // 注意：这不是最佳实践，但在某些情况下可能需要，可考虑使用 Espresso idling resources
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            // 验证是否跳转到了MainActivity2
            intended(hasComponent(MainActivity2.class.getName()));
        }
    }
}